package rsj.api.product;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.TableNode;
import common.util.Assert;
import common.util.BoxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.api.user.entity.UserAccount;
import rsj.context.ApplicationContextUtil;
import rsj.service.ProductService;
import rsj.service.user.IUserService;
import rsj.sql.user.UserDao;

@RestController
public class GetHomePageData extends BaseLink {

    @Autowired
    private ProductService productService;
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "rsj_prod_show")
    public Box executeLink(Box in){
        String orgId = getAndCheckOrgId(in);
        TableNode productTypeAll = productService.getHomePageData(orgId);

        return BoxUtil.successBox(in, "成功获取数据！","Data_List", productTypeAll.toJson());
    }

    private String getAndCheckOrgId(Box in) {
        MapNode user = in.getMapNode("$user");
        String userId = user.getValue("User_Id");
        Assert.isNotBlank( userId, "用户id不能为空！");
        UserAccount userInfo = userService.getUserInfo(userId);
        return userInfo.getUserOrgId();
    }

}
